package com.onetime.mall.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onetime.mall.trading.dao.AdminDao;
import com.onetime.mall.trading.entity.AdminEntity;
import com.onetime.mall.trading.service.AdminService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<AdminEntity> wrapper = new QueryWrapper<>();
        wrapper.like("admin_name",key).or().like("account_number",key);

        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 添加新的管理员
     *
     * @param adminEntity 新管理员
     * @return true or false
     */
    @Override
    public boolean addAdmin(AdminEntity adminEntity) {
        return baseMapper.insert(adminEntity) == 1;
    }

    /**
     * 管理员登录
     *
     * @param accountNumber 账号
     * @param adminPassword 密码
     * @return 一个管理员
     */
    @Override
    public AdminEntity login(String accountNumber, String adminPassword) {
        QueryWrapper<AdminEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("account_number", accountNumber).eq("admin_password", adminPassword);
        return baseMapper.selectOne(wrapper);
    }

}